<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../../common/import.jsp" %>
<%@ taglib uri="/WEB-INF/privilege" prefix="p"%>

<html>
  <head>
  </head>
  <body scroll="no">
  <script type="text/javascript">
       jQuery.ajaxSetup({cache: false});
       $(function(){
    	   $("#userTable").datagrid({
    		   //width: 'auto',
    		   nowrap: false,
    		   triped: false,
    		   checkOnSelect: true,
    		   fitColumns: true,
    		   url: '${ctx}/sys/user/list.do',
    		   columns: [[
    		             {field: 'ck', checkbox: true},
    		             {field: 'userName', title: '用户名', width: 100, align: 'center'},
    		             {field: 'name', title: '姓名', width: 100, align: 'center'},
    		             {field: 'sex', title: '性别', width: 40, align: 'center',
    		            	 formatter:function(value){
 						    	if (value=='1'){
 							    	return '男';
 						    	} else if(value=='2'){
 							    	return '女';
 						    	} else {
 						    		return '';
 						    	}
 		            	 }},
    		             {field: 'phone', title: '电话', width: 100, align: 'center'},
    		             {field: 'status', title: '状态', width: 40, align: 'center',
    		            	 formatter:function(value){
    						    	if (value=='1'){
    							    	return '<span class="icon icon-used">&nbsp;</span>';
    						    	} else if(value=='0'){
    							    	return '<span class="icon icon-not-used">&nbsp;</span>';
    						    	} else {
    						    		return '';
    						    	}
    		            	 }
    		             },
    		             {field: 'organName', title: '机构名称', width: 100, align: 'center'},
    		             {field: 'createUser', title: '创建者', width: 60, align: 'center'},
    		             {field: 'createTime', title: '创建时间', width: 100, align: 'center'},
    		             {field: 'loginIp', title: '登录IP', width: 100, align: 'center'},
    		             {field: 'loginTime', title: '登录时间', width: 100, align: 'center'}//,
    		             //{field: 'aaa', title: '操作', width: 120, align: 'left',
    		            //	 formatter:function(value,rowData,rowIndex){
    		            //		 return '<p:op code="sysUserEdit"><a href="${ctx}/sys/user/toAdd.do" onclick="return edit(this.href);">修改</a></p:op>';
    		            //	 }}
    		            ]],
    		   pagination: true,
    		   rownumbers: true,
    		   pageSize: 20,
    		   pageList: pageList,
    		   queryParams: {}//,
    		   //toolbar: '#optb'
    	   });
       });
       function reloadGrid(){
    	   $("#userTable").datagrid("reload");
       }
       function loadGrid(){
    	   $("#userTable").datagrid("load");
       }
       function getSelectedRows(){
    	   var rows = $('#userTable').datagrid('getSelections');
    	   return rows;
       }
       function queryForm(){
    	   var params = $('#userTable').datagrid('options').queryParams; //先取得 datagrid 的查询参数
    	   var fields =$('#queryForm').serializeArray(); //自动序列化表单元素为JSON对象
    	   $.each( fields, function(i, field){
    		   params[field.name] = field.value; //设置查询参数
           });   
    	   loadGrid();
       }
       function clearForm(){
    	   $('#queryForm').form('clear');
    	   queryForm();
       }
       
       function add(href){
    	   showWindow("添加用户", href);
    	   return false;
       }
       function edit(href){
    	   var node = getSelectedRows();
    	   if (node&&node.length==1){
    		   showWindow("修改用户", href + "?id="+node[0].id);
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       function del(href){
    	   var rows = getSelectedRows();
    	   if (rows&&rows.length > 0){
    		   deleteConfirm(function(){
    			   var ps = "";
		           $.each(rows,function(i,n){
		        	   if(i==0) 
		        			ps += n.id;
		        		else
		        			ps += ","+n.id;
		        	});
		           $.blockUI();
    			   $.ajax({
		    			  url: href,   // 提交的页面
		    		      data: {ids: ps}, 
		    		      type: "POST",
		    		      success: function(data) {
		    		    	  if(data.success==true){
		    		        	showSuccessMsg();
		    		          }else{
		    		        	showFailAlert(data.msg);
		    		          }
		    		    	  reloadGrid();
		    		    	  $.unblockUI();
		    		      },
		    		      error: function(xhr) {
		    		    	  $.unblockUI();
		    		    	  showFailAlert2(xhr);
		    		      }
		    		  })
    		   });
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       
       function assignRole(href){
    	   var rows = getSelectedRows();
    	   if (rows&&rows.length==1){
    		   showWindow("分配角色", href+"?id="+rows[0].id);
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       function view(href){
    	   var rows = getSelectedRows();
    	   if (rows&&rows.length==1){
    		   showWindow("查看用户", href+"?id="+rows[0].id);
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       function initPwd(href){
    	   var rows = getSelectedRows();
    	   if (rows&&rows.length > 0){
    		   showConfirm('温馨提示', '确定要初始化用户的密码为<font color="red">123456</font>吗?', function(){
    			   var ps = "";
		           $.each(rows,function(i,n){
		        	   if(i==0) 
		        			ps += n.id;
		        		else
		        			ps += ","+n.id;
		        	});
    			   $.ajax({
		    			  url: href,   // 提交的页面
		    		      data: {ids: ps}, 
		    		      type: "POST",
		    		      success: function(data) {
		    		    	  reloadGrid();
		    		    	  if(data.success==true){
		    		    		  showSuccessMsg();
		    		    	  }else{
		    		        	  showFailAlert(data.msg);
		    		          }
		    		      },
		    		      error: function(xhr) {
		    		    	  if(xhr.status != 911&&xhr.status!= 912){
		    		    		  showFailAlert()
		    		    	  }
		    		      }
		    		  })
    		   });
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
    </script>
  <div class="easyui-layout" data-options="fit:true">
  <div region="north" style="overflow-y: hidden; overflow-x: hidden" border="false">
    <form id="queryForm" onSubmit="queryForm();return false;">
        <table width="99.5%" style="padding-left: 5px;">
	        <td width="40px">用户名</td><td width="60px"><input id="userName" type="text" name="userName" value="" /></td>
		    <td width="85px"><a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" onclick="queryForm();">查询</a></td>
		    <td><a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" onclick="clearForm();">重置</a></td>
		</table>
    </form>
    <div id="optb" class="btabs">
        <p:op code="sysUserAdd">
	        <a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/user/toAdd.do" 
	        onclick="return add(this.href);"><span class="icon icon-add">&nbsp;</span>添加</a>
	    </p:op>
	    <p:op code="sysUserEdit">
	        <a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/user/toAdd.do" 
	        onclick="return edit(this.href);"><span class="icon icon-edit">&nbsp;</span>修改</a> 
	    </p:op>
	    <p:op code="sysUserDel">
	        <a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/user/delete.do" 
	        onclick="return del(this.href);"><span class="icon icon-delete">&nbsp;</span>删除</a> 
	    </p:op>
	    <p:op code="sysUserView">
	        <a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/user/toView.do" 
	        onclick="return view(this.href);"><span class="icon icon-view">&nbsp;</span>查看</a> 
	    </p:op>
	    <p:op code="sysUserAssignRole">
	        <a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/user/toAssignRoles.do" 
	        onclick="return assignRole(this.href);"><span class="icon icon-assignRole">&nbsp;</span>分配角色</a>
	    </p:op>
	    <p:op code="sysUserInitPwd">
	        <a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/user/initUserPwd.do" 
	        onclick="return initPwd(this.href);"><span class="icon icon-initPwd">&nbsp;</span>初始化密码</a>
	    </p:op>
    </div>
    </div>
    <div region="center" style="padding: 1px;" border="false">
       <table id="userTable" fit="true"></table>
    </div>
    </div>
  </body>
</html>
